﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.IO;

namespace WordToSharePoint
{
    public class XSLFileConduit
    {
        public string GetXSL()
        {
            StreamReader r = new StreamReader(Assembly.GetAssembly(this.GetType()).GetManifestResourceStream("WordToSharePoint.DocxToHTMLWithImagesShort.xslt"));
            string xsl = r.ReadToEnd();
            return xsl;
        }

        public static string GetMinimalXSL()
        {
            return @"

            <xsl:stylesheet
                            xmlns:xsl=\u0x0022http://www.w3.org/1999/XSL/Transform\u0x0022 version=\u0x00221.0\u0x0022
                            xmlns:w=\u0x0022http://schemas.openxmlformats.org/wordprocessingml/2006/main\u0x0022
                            xmlns:wp=\u0x0022http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\u0x0022
                            xmlns:r=\u0x0022http://schemas.openxmlformats.org/officeDocument/2006/relationships\u0x0022
                            xmlns:rel=\u0x0022http://schemas.openxmlformats.org/package/2006/relationships\u0x0022
                            xmlns:a=\u0x0022http://schemas.openxmlformats.org/drawingml/2006/main\u0x0022
                            xmlns:v=\u0x0022urn:schemas-microsoft-com:vml\u0x0022
            >

              <xsl:output method=\u0x0022html\u0x0022 />
              
              <xsl:template match=\u0x0022/w:document\u0x0022>
                <xsl:apply-templates select=\u0x0022w:body\u0x0022 />
              </xsl:template>
              
              <xsl:template match=\u0x0022w:body\u0x0022>
                <html>
                  <body>
                    <xsl:apply-templates select=\u0x0022w:p\u0x0022 />
                    <xsl:apply-templates select=\u0x0022w:tbl\u0x0022/>
                  </body>
                </html>
              </xsl:template>

              <xsl:template match=\u0x0022w:tbl\u0x0022>
                <TABLE width=\u0x002270%\u0x0022>
                  <xsl:apply-templates select=\u0x0022w:tr\u0x0022/>
                </TABLE>
              </xsl:template>

              <xsl:template match=\u0x0022w:tr\u0x0022>
                <TR>
                  <xsl:apply-templates select=\u0x0022w:tc\u0x0022/>
                </TR>
              </xsl:template>

              <xsl:template match=\u0x0022w:tc\u0x0022>
                <xsl:element name=\u0x0022td\u0x0022>
                  <xsl:apply-templates select=\u0x0022w:pPr\u0x0022 />
                  <xsl:apply-templates select=\u0x0022w:p\u0x0022/>
                </xsl:element>
              </xsl:template>
              
              <xsl:template match=\u0x0022w:p\u0x0022>
                <p>      
                  <xsl:attribute name=\u0x0022class\u0x0022>
                    <xsl:value-of select=\u0x0022w:pPr/w:pStyle//@w:val\u0x0022/>
                  </xsl:attribute>
                  
                  <xsl:apply-templates select=\u0x0022w:r\u0x0022 />
                </p>
              </xsl:template>
                
              <xsl:template match=\u0x0022w:jc\u0x0022>
                text-align:
                <xsl:value-of select=\u0x0022@w:val\u0x0022 />
              </xsl:template>
              
              <xsl:template match=\u0x0022w:r\u0x0022>
                
                  <xsl:value-of select=\u0x0022w:t\u0x0022 />
                  <xsl:apply-templates select=\u0x0022w:drawing//a:blip\u0x0022 />
                  <xsl:apply-templates select=\u0x0022w:pict//v:shape//v:imagedata\u0x0022 />
                  <xsl:apply-templates select=\u0x0022w:object//v:shape//v:imagedata\u0x0022 />      
                
              </xsl:template>

              <xsl:template match=\u0x0022w:drawing//a:blip\u0x0022>
                <xsl:variable name=\u0x0022w\u0x0022>
                  <xsl:value-of select=\u0x0022.//wp:extent/@cx\u0x0022/>
                </xsl:variable>

                <xsl:variable name=\u0x0022h\u0x0022>
                  <xsl:value-of select=\u0x0022.//wp:extent/@cy\u0x0022/>
                </xsl:variable>

                <img src=\u0x0022{@imagePath}\u0x0022>

                </img>

              </xsl:template>

              <xsl:template match=\u0x0022w:pict//v:shape//v:imagedata\u0x0022>

                <xsl:variable name=\u0x0022w\u0x0022>
                  <xsl:value-of select=\u0x0022.//wp:extent/@cx\u0x0022/>
                </xsl:variable>

                <xsl:variable name=\u0x0022h\u0x0022>
                  <xsl:value-of select=\u0x0022.//wp:extent/@cy\u0x0022/>
                </xsl:variable>

                <img src=\u0x0022{@imagePath}\u0x0022>
                </img>

                <xsl:apply-templates select=\u0x0022v:shape\u0x0022 />
              </xsl:template>

              <xsl:template match=\u0x0022w:object//v:shape//v:imagedata\u0x0022>

                <xsl:variable name=\u0x0022w\u0x0022>
                  <xsl:value-of select=\u0x0022.//wp:extent/@cx\u0x0022/>
                </xsl:variable>

                <xsl:variable name=\u0x0022h\u0x0022>
                  <xsl:value-of select=\u0x0022.//wp:extent/@cy\u0x0022/>
                </xsl:variable>

                <img src=\u0x0022{@imagePath}\u0x0022>
                </img>

                <xsl:apply-templates select=\u0x0022v:shape\u0x0022 />
              </xsl:template>
              
              <xsl:template match=\u0x0022w:sz\u0x0022>
                font-size:
                <xsl:value-of select=\u0x0022@w:val\u0x0022 />
                px;
              </xsl:template>
              
              <xsl:template match=\u0x0022w:vertAlign\u0x0022>
                <xsl:variable name=\u0x0022jcVal\u0x0022 select=\u0x0022@w:val\u0x0022 />
                <xsl:if test=\u0x0022$jcVal = 'superscript'\u0x0022>
                  font-size:33%;position:relative;bottom:0.5em;
                </xsl:if>
                <xsl:if test=\u0x0022$jcVal = 'subscript'\u0x0022>
                  font-size:33%;position:relative;bottom:-0.5em;
                </xsl:if>
              </xsl:template>
              
              <xsl:template match=\u0x0022w:b\u0x0022>font-weight:bold;</xsl:template>
              <xsl:template match=\u0x0022w:i\u0x0022>font-style:italic;</xsl:template>
              <xsl:template match=\u0x0022w:u\u0x0022>text-decoration:underline;</xsl:template>

            </xsl:stylesheet>

            ".Replace(@"\u0x0022", ((char)34).ToString());
        }


        public static string GetRichHTMLXSL()
        {
            return @"
                <xsl:stylesheet
                                xmlns:xsl=\u0x0022http://www.w3.org/1999/XSL/Transform\u0x0022 version=\u0x00221.0\u0x0022
                                xmlns:w=\u0x0022http://schemas.openxmlformats.org/wordprocessingml/2006/main\u0x0022
                                xmlns:wp=\u0x0022http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\u0x0022
                                xmlns:r=\u0x0022http://schemas.openxmlformats.org/officeDocument/2006/relationships\u0x0022
                                xmlns:rel=\u0x0022http://schemas.openxmlformats.org/package/2006/relationships\u0x0022
                                xmlns:a=\u0x0022http://schemas.openxmlformats.org/drawingml/2006/main\u0x0022
                                xmlns:v=\u0x0022urn:schemas-microsoft-com:vml\u0x0022
                >

                  <xsl:output method=\u0x0022html\u0x0022 />
                  
                  <xsl:template match=\u0x0022/w:document\u0x0022>
                    <xsl:apply-templates select=\u0x0022w:body\u0x0022 />
                  </xsl:template>
                  
                  <xsl:template match=\u0x0022w:body\u0x0022>
                    <html>
                      <body>
                        <xsl:apply-templates select=\u0x0022w:p\u0x0022 />
                        <xsl:apply-templates select=\u0x0022w:tbl\u0x0022/>
                      </body>
                    </html>
                  </xsl:template>


                  <xsl:template match=\u0x0022w:tbl\u0x0022>
                    <TABLE width=\u0x002270%\u0x0022>
                      <xsl:apply-templates select=\u0x0022w:tr\u0x0022/>
                    </TABLE>
                  </xsl:template>

                  <xsl:template match=\u0x0022w:tr\u0x0022>
                    <TR>
                      <xsl:apply-templates select=\u0x0022w:tc\u0x0022/>
                    </TR>
                  </xsl:template>

                  <xsl:template match=\u0x0022w:tc\u0x0022>
                    <xsl:element name=\u0x0022td\u0x0022>
                      <xsl:apply-templates select=\u0x0022w:pPr\u0x0022 />
                      <xsl:apply-templates select=\u0x0022w:p\u0x0022/>
                    </xsl:element>
                  </xsl:template>
                  
                  <xsl:template match=\u0x0022w:p\u0x0022>
                    <p>      
                      <xsl:attribute name=\u0x0022class\u0x0022>
                        <xsl:value-of select=\u0x0022w:pPr/w:pStyle//@w:val\u0x0022/>
                      </xsl:attribute>
                      
                      <xsl:apply-templates select=\u0x0022w:r\u0x0022 />
                    </p>
                  </xsl:template>
                    
                  <xsl:template match=\u0x0022w:jc\u0x0022>
                    text-align:
                    <xsl:value-of select=\u0x0022@w:val\u0x0022 />
                  </xsl:template>
                  
                  <xsl:template match=\u0x0022w:r\u0x0022>
                    
                      <xsl:apply-templates select=\u0x0022w:t\u0x0022 />
                      <xsl:apply-templates select=\u0x0022w:drawing//a:blip\u0x0022 />
                      <xsl:apply-templates select=\u0x0022w:pict//v:shape//v:imagedata\u0x0022 />
                      <xsl:apply-templates select=\u0x0022w:object//v:shape//v:imagedata\u0x0022 />      
                    
                  </xsl:template>

                  <xsl:template match=\u0x0022w:t\u0x0022>
                    <span>
                      <xsl:apply-templates select=\u0x0022../w:rPr\u0x0022 />
                      <xsl:value-of select=\u0x0022.\u0x0022 />
                    </span>
                  </xsl:template>

                  <xsl:template match=\u0x0022w:rPr\u0x0022>
                    <xsl:attribute name=\u0x0022style\u0x0022>
                      <xsl:apply-templates />
                   </xsl:attribute>
                  </xsl:template>

                  <xsl:template match=\u0x0022w:u\u0x0022>text-decoration:underline;</xsl:template>
                  <xsl:template match=\u0x0022w:b\u0x0022>font-weight:bold;</xsl:template>
                  <xsl:template match=\u0x0022w:i\u0x0022>font-style:italic;</xsl:template>

                  <xsl:template match=\u0x0022w:drawing//a:blip\u0x0022>
                    <xsl:variable name=\u0x0022w\u0x0022>
                      <xsl:value-of select=\u0x0022.//wp:extent/@cx\u0x0022/>
                    </xsl:variable>

                    <xsl:variable name=\u0x0022h\u0x0022>
                      <xsl:value-of select=\u0x0022.//wp:extent/@cy\u0x0022/>
                    </xsl:variable>

                    <img src=\u0x0022{@imagePath}\u0x0022>

                    </img>

                  </xsl:template>

                  <xsl:template match=\u0x0022w:pict//v:shape//v:imagedata\u0x0022>

                    <xsl:variable name=\u0x0022w\u0x0022>
                      <xsl:value-of select=\u0x0022.//wp:extent/@cx\u0x0022/>
                    </xsl:variable>

                    <xsl:variable name=\u0x0022h\u0x0022>
                      <xsl:value-of select=\u0x0022.//wp:extent/@cy\u0x0022/>
                    </xsl:variable>

                    <img src=\u0x0022{@imagePath}\u0x0022>
                    </img>

                    <xsl:apply-templates select=\u0x0022v:shape\u0x0022 />
                  </xsl:template>

                  <xsl:template match=\u0x0022w:object//v:shape//v:imagedata\u0x0022>

                    <xsl:variable name=\u0x0022w\u0x0022>
                      <xsl:value-of select=\u0x0022.//wp:extent/@cx\u0x0022/>
                    </xsl:variable>

                    <xsl:variable name=\u0x0022h\u0x0022>
                      <xsl:value-of select=\u0x0022.//wp:extent/@cy\u0x0022/>
                    </xsl:variable>

                    <img src=\u0x0022{@imagePath}\u0x0022>
                    </img>

                    <xsl:apply-templates select=\u0x0022v:shape\u0x0022 />
                  </xsl:template>
                  
                  <xsl:template match=\u0x0022w:sz\u0x0022>
                    font-size:
                    <xsl:value-of select=\u0x0022@w:val\u0x0022 />
                    px;
                  </xsl:template>
                  
                  <xsl:template match=\u0x0022w:vertAlign\u0x0022>
                    <xsl:variable name=\u0x0022jcVal\u0x0022 select=\u0x0022@w:val\u0x0022 />
                    <xsl:if test=\u0x0022$jcVal = 'superscript'\u0x0022>
                      font-size:33%;position:relative;bottom:0.5em;
                    </xsl:if>
                    <xsl:if test=\u0x0022$jcVal = 'subscript'\u0x0022>
                      font-size:33%;position:relative;bottom:-0.5em;
                    </xsl:if>
                  </xsl:template>
                  
                  <xsl:template match=\u0x0022w:b\u0x0022>font-weight:bold;</xsl:template>
                  <xsl:template match=\u0x0022w:i\u0x0022>font-style:italic;</xsl:template>
                  <xsl:template match=\u0x0022w:u\u0x0022>text-decoration:underline;</xsl:template>

                </xsl:stylesheet>


                ".Replace(@"\u0x0022", ((char)34).ToString());
        }
    }
}
